package com.atguigu.yygh.orders.service.impl;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.orders.mapper.OrderInfoMapper;
import com.atguigu.yygh.orders.mapper.PaymentInfoMapper;
import com.atguigu.yygh.orders.service.OrderInfoService;
import com.atguigu.yygh.orders.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author 卓抱抱
 * @since 2022-03-19
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    //根据id值查询订单
    @Override
    public PaymentInfo getPaymentInfo(Long orderId) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);

        return paymentInfo;
    }

    // 生成二维码订单之后 我们需要定时往微信接口进行调取 判断是否成功支付
    @Override
    public void paySuccess(Integer status, Map map) {
        // 从map中获取订单单号
        String out_trade_no = (String) map.get("out_trade_no");

        // 进行订单单号中数据更新
        // 更新操作一般都是先查询
        QueryWrapper<OrderInfo> orderInfoWrapper = new QueryWrapper<>();
        orderInfoWrapper.eq("out_trade_no", out_trade_no);
        OrderInfo orderInfo = orderInfoMapper.selectOne(orderInfoWrapper);

        // 判断是否为空
        if (StringUtils.isEmpty(orderInfo)) {
            throw new YyghException(20001, "查询不到该订单详情");
        }
        // 不为空进行数据更新
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderInfoMapper.updateById(orderInfo);
        // 进行支付订单数据更新
        QueryWrapper<PaymentInfo> paymentInfoWrapper = new QueryWrapper<>();
        paymentInfoWrapper.eq("out_trade_no", out_trade_no);
        PaymentInfo paymentInfo = baseMapper.selectOne(paymentInfoWrapper);
        // 判断是否为空
        if (StringUtils.isEmpty(paymentInfo)) {
            throw new YyghException(20001, "查询不到该支付详情");
        }
        // 不为空进行数据更新
        String transaction_id = (String) map.get("transaction_id");
        paymentInfo.setTradeNo(transaction_id);
        paymentInfo.setUpdateTime(new Date());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(map.toString());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());

        baseMapper.updateById(paymentInfo);
    }


    //保存订单
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        // 保存订单的前提就是查询订单
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderInfo.getId());
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer > 0) {
            return;
        }
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        // 定义的微信支付或者支付宝支付
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        // 支付中，因为在订单中我么需要保存客户每一次下订单的记录，防止客户后期出现问题，我们方便解决
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + "|" + orderInfo.getHosname() + "|" + orderInfo.getDepname() + "|" + orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }
}
